# -*- encoding: utf-8 -*-

from osv import osv, fields
class contract_contract(osv.osv):
    _name = 'contract'
    _description = '合同'
    def get_employee(self, cr, uid, context={}):
        obj = self.pool.get('hr.employee')
        ids = obj.search(cr, uid, [('user_id','=',uid)])
        res = obj.read(cr, uid, ids, ['id','name'], context)
        return res and res[0]['id'] or 0

    _columns = {
        'proposer': fields.many2one('hr.employee', '申请人', required=True),
        'auditor': fields.many2one('hr.employee', '审核人', required=False),
        'contractNo': fields.char('合同编号', size=20, help='合同的编号'),
        'name': fields.char('合同标题', size=50, help='合同的标题'),
        'partyA': fields.char('合同甲方', size=50, help='合同甲方'),
        'partyB': fields.char('合同乙方', size=50, help='合同乙方'),
        'amount': fields.float('合同金额', help='合同总金额'),
        'contents': fields.text('合同内容', help='合同内容简介，完整内容请见附件'),
        'startDate': fields.date('合同开始时间', help='合同生效的时间'),
        'endDate': fields.date('合同结束时间', help='合同预期结束或失效的时间'),
        'type': fields.selection([('sale','销售'),('purchase','采购')], '合同类型', help='合同类型'),
        'active': fields.boolean('有效'),
        'state': fields.selection([('draft','草稿'),('wait_prove','待签署'),('proved','已生效'),('rejected','被作废')], '状态'),
    }
    _defaults = {
        'proposer': lambda self,cr,uid,context: self.get_employee(cr,uid,context),
        'active': lambda *a: 1,
        'state': lambda *a: 'draft',
    }
    
    def action_state_proved(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        
        self.write(cr, uid, ids, {'auditor':uid}, context=context)
        for inv_id, name in self.name_get(cr, uid, ids, context=context):
            message = ("Contract '%s' is proved.") % name
            self.log(cr, uid, inv_id, message)
        return True

    def action_state_rejected(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        
        self.write(cr, uid, ids, {'auditor':uid}, context=context)
        for inv_id, name in self.name_get(cr, uid, ids, context=context):
            message = ("Contract '%s' is rejected.") % name
            self.log(cr, uid, inv_id, message)
        return True

contract_contract()
